﻿using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

public class PbfBookmarkList
{
  public List<PbfBookmark> list = new List<PbfBookmark>();
  string sp = new string('=', 110);
  string sp2 = new string('-', 110);

  public PbfBookmarkList(IEnumerable<string> fpaths)
  {
    foreach (var pbfFile in fpaths)
    {
      PbfBookmark pbf = new PbfBookmark(pbfFile);
      list.Add(pbf);
    }
  }

  public string ToCmdList()
  {
    StringBuilder sb = new StringBuilder();
    sb.AppendLine($"@echo off");
    sb.AppendLine($"chcp 936");
    sb.AppendLine($"echo.");
    sb.AppendLine($"echo   [ Potplayer bookmark *.pbf -- / movie / part / clip ]");
    sb.AppendLine($"echo.");
    sb.AppendLine();
    for (int i = 0; i < list.Count; i++)
    {
      PbfBookmark pbf = list[i];
      if (pbf.lsVideoPath.Count > 0)
      {
        sb.AppendLine($"echo {sp}");
        sb.AppendLine($"echo   File:  {pbf.pbfpath}");
        string v1 = Path.GetFileName(pbf.lsVideoPath.First());
        sb.AppendLine($"echo   Video: {v1}");
        sb.AppendLine($"echo {sp}");
        sb.AppendLine("");
        for (int j = 0; j < pbf.timestamps.Count; j++)
        {
          ffmepgCmdInfo v = pbf.timestamps[j];
          var dir = Path.GetDirectoryName(v1);
          var fn = Path.GetFileNameWithoutExtension(v1);
          var ext = Path.GetExtension(v1);
          var sstoname = v.ssto.Replace("-", "_").Replace(" ", "").Replace(":", "");
          var fne = Path.Combine(dir, fn + sstoname + ext);
          sb.AppendLine("echo.");
          sb.AppendLine($"echo {sp2}");
          sb.AppendLine($"echo   Bookmark: {v.bookmark2}");
          //sb.AppendLine($"echo   Command:  ffmpeg -hide_banner -i \"{v1}\" {v.ssto} -c:v copy -n \"xxx_ssto.{ext}\"");
          sb.AppendLine($"echo   Ssto:     {v.ssto}");
          sb.AppendLine($"echo   File:     {i + 1:D2} / {list.Count:D2}");
          sb.AppendLine($"echo   Index:    {j + 1:D2} / {pbf.timestamps.Count:D2}");
          sb.AppendLine($"echo {sp2}");
          var fi = Path.Combine(v.dir, v.nameHasmark);
          if (File.Exists(fi))
          {
            sb.AppendLine($"{v.cmdRemHasmark}");
          }
          else
          {
            sb.AppendLine($"{v.cmdHasmark}");
          }
          sb.AppendLine("echo.");
        }
      }
    }
    return sb.ToString();
  }

  public override string ToString()
  {
    StringBuilder stringBuilder = new StringBuilder();
    stringBuilder.AppendLine($"{sp}");
    stringBuilder.AppendLine("                  Generated Timestamps");
    stringBuilder.AppendLine($"{sp}");

    stringBuilder.AppendLine($"  Total Pbf files: {list.Count}");
    stringBuilder.AppendLine();
    foreach (var pbf in list)
    {
      stringBuilder.AppendLine(pbf.ToString());
    }
    stringBuilder.AppendLine($"{sp}");
    return stringBuilder.ToString();
  }
}

